<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>健康医疗</title>
        <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"/>
        <link rel="shortcut icon" href="../img/favicon.ico"/>
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../element-ui/index.css">
        <link rel="stylesheet" href="../css/style.css">
        <link rel="stylesheet" href="../css/font-awesome/css/font-awesome.min.css">
        <style>
            .avatar-uploader .el-upload {
                border: 1px dashed #d9d9d9;
                border-radius: 6px;
                cursor: pointer;
                position: relative;
                overflow: hidden;
            }

            .avatar-uploader .el-upload:hover {
                border-color: #409EFF;
            }

            .avatar-uploader-icon {
                font-size: 28px;
                color: #8c939d;
                width: 178px;
                height: 178px;
                line-height: 178px;
                text-align: center;
            }

            .avatar {
                width: 178px;
                height: 178px;
                display: block;
            }

            .datatable {
                position: relative;
                box-sizing: border-box;
                -webkit-box-flex: 1;
                width: 100%;
                max-width: 100%;
                font-size: 14px;
                color: rgb(96, 98, 102);
                overflow: hidden;
                flex: 1 1 0%;
            }

            .datatable td, .datatable th {
                padding: 12px 0;
                min-width: 0;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                text-overflow: ellipsis;
                vertical-align: middle;
                position: relative;
                text-align: left;
            }

            .checkScrol::-webkit-scrollbar {
                display: none;
            }
        </style>
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>预约管理<small>套餐管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item>首页</el-breadcrumb-item>
                    <el-breadcrumb-item>预约管理</el-breadcrumb-item>
                    <el-breadcrumb-item>套餐管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="编码/名称/助记码" v-model="queryString" style="width: 200px;"
                                  class="filter-item" @keyup.enter.native="handleCurrentChange(1)" clearable></el-input>
                        <el-button @click="handleCurrentChange(1)" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="left" width="60" label="序号"></el-table-column>
                        <el-table-column prop="code" label="套餐编码" width="80" align="left"></el-table-column>
                        <el-table-column prop="name" label="套餐名称" align="left"></el-table-column>
                        <el-table-column label="适用性别" width="80" align="left">
                            <template slot-scope="scope">
                                <span>{{ scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '男' : '女'}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="age" width="100" label="适用年龄" align="left"></el-table-column>
                        <el-table-column prop="helpCode" width="80" label="助记码" align="left"></el-table-column>
                        <el-table-column prop="remark" label="说明" align="left"></el-table-column>
                        <el-table-column label="操作" width="160" align="left">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row.id)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination class="pagiantion"
                                       :current-page="pageNow"
                                       :page-size="pageSize"
                                       :total="total"
                                       layout="total, prev, pager, next, jumper"
                                       @current-change="handleCurrentChange">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹出层 -->
                    <div class="add-form">
                        <el-dialog title="新增套餐" :visible.sync="dialogFormVisible">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form ref="dataAddForm" :model="formData" :rules="rules"
                                                 label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="编码" prop="code">
                                                        <el-input v-model="formData.code"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="名称" prop="name">
                                                        <el-input v-model="formData.name"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="适用性别">
                                                        <el-select v-model="formData.sex">
                                                            <el-option label="不限" value="0"></el-option>
                                                            <el-option label="男" value="1"></el-option>
                                                            <el-option label="女" value="2"></el-option>
                                                        </el-select>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="助记码">
                                                        <el-input v-model="formData.helpCode"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="套餐价格">
                                                        <el-input type="number" v-model="formData.price"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="适用年龄">
                                                        <el-input v-model="formData.age"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="上传图片">
                                                        <el-upload
                                                                class="avatar-uploader"
                                                                action="../setMeal/upload"
                                                                :auto-upload="autoUpload"
                                                                name="imgFile"
                                                                :show-file-list="false"
                                                                :on-success="handleAvatarSuccess"
                                                                :before-upload="beforeAvatarUpload"
                                                                v-loading="loading">
                                                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                                        </el-upload>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="说明">
                                                        <el-input v-model="formData.remark" type="textarea"></el-input>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="注意事项">
                                                        <el-input v-model="formData.attention"
                                                                  type="textarea"></el-input>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                    </el-tab-pane>
                                    <el-tab-pane label="检查组信息" name="second">
                                        <div class="checkScrol">
                                            <div class="filter-container">
                                                <el-input v-model="queryCheckGroup" placeholder="项目编码/项目名称"
                                                          style="width: 200px;"
                                                          class="filter-item" @keyup.enter.native="handleCheckGroup"
                                                          clearable>
                                                </el-input>
                                                <el-button @click="handleCheckGroup" class="dalfBut">查询</el-button>
                                            </div>
                                            <el-table ref="add-multipleTable" size="small" current-row-key="id"
                                                      :data="tableData" stripe
                                                      height="200px" row-key="id"
                                                      @selection-change="handleSelectionChange">
                                                <el-table-column type="selection"
                                                                 :reserve-selection="true"></el-table-column>
                                                <el-table-column prop="code" label="项目编码"
                                                                 align="center"></el-table-column>
                                                <el-table-column prop="name" label="项目名称"
                                                                 align="center"></el-table-column>
                                                <el-table-column prop="remark" label="项目说明"
                                                                 align="center"></el-table-column>
                                            </el-table>
                                        </div>
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                    <!-- 编辑标签弹出层 -->
                    <div class="add-form">
                        <el-dialog title="编辑套餐" :visible.sync="dialogFormVisible4Edit">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="基本信息" name="first">
                                        <el-form ref="dataEditForm" :model="formData" :rules="rules"
                                                 label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="编码" prop="code">
                                                        <el-input v-model="formData.code"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="名称" prop="name">
                                                        <el-input v-model="formData.name"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="适用性别">
                                                        <el-select v-model="formData.sex">
                                                            <el-option label="不限" value="0"></el-option>
                                                            <el-option label="男" value="1"></el-option>
                                                            <el-option label="女" value="2"></el-option>
                                                        </el-select>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="助记码">
                                                        <el-input v-model="formData.helpCode"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="套餐价格">
                                                        <el-input type="number" v-model="formData.price"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="适用年龄">
                                                        <el-input v-model="formData.age"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="上传图片">
                                                        <el-upload
                                                                class="avatar-uploader"
                                                                action="../setMeal/upload"
                                                                :auto-upload="autoUpload"
                                                                name="imgFile"
                                                                :show-file-list="false"
                                                                :on-success="handleAvatarSuccess"
                                                                :before-upload="beforeAvatarUpload"
                                                                v-loading="loading">
                                                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                                        </el-upload>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="说明">
                                                        <el-input v-model="formData.remark" type="textarea"></el-input>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="24">
                                                    <el-form-item label="注意事项">
                                                        <el-input v-model="formData.attention"
                                                                  type="textarea"></el-input>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                    </el-tab-pane>
                                    <el-tab-pane label="检查组信息" name="second">
                                        <div class="checkScrol">
                                            <div class="filter-container">
                                                <el-input v-model="queryCheckGroup" placeholder="项目编码/项目名称"
                                                          style="width: 200px;"
                                                          class="filter-item" @keyup.enter.native="handleCheckGroup"
                                                          clearable>
                                                </el-input>
                                                <el-button @click="handleCheckGroup" class="dalfBut">查询</el-button>
                                            </div>
                                            <el-table ref="edit-multipleTable" size="small" current-row-key="id"
                                                      :data="tableData" stripe
                                                      height="200px" row-key="id"
                                                      @selection-change="handleSelectionChange">
                                                <el-table-column type="selection"
                                                                 :reserve-selection="true"></el-table-column>
                                                <el-table-column prop="code" label="项目编码"
                                                                 align="center"></el-table-column>
                                                <el-table-column prop="name" label="项目名称"
                                                                 align="center"></el-table-column>
                                                <el-table-column prop="remark" label="项目说明"
                                                                 align="center"></el-table-column>
                                            </el-table>
                                        </div>
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>

    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../js/axios.min.js"></script>
    <script src="../element-ui/index.js"></script>
    <script type="text/javascript" src="../js/jquery-3.5.1.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data: {
                loading: false,//局部遮罩
                pageNow: 1,//当前页码
                pageSize: 8,//每页显示的记录数
                total: 0,//总记录数
                queryString: "",//查询条件-套餐
                queryCheckGroup: "",//查询条件-检查组
                dataList: [],//列表数据
                formData: {},//表单数据
                tableData: [],//添加表单窗口中检查组列表数据
                checkgroupIds: [],//添加表单窗口中检查组复选框对应id
                autoUpload: true,//自动上传
                imageUrl: null,//模型数据，用于上传图片完成后图片预览
                activeName: 'first',//添加/编辑窗口Tab标签名称
                dialogFormVisible: false,//控制添加窗口显示/隐藏
                dialogFormVisible4Edit: false, // 控制编辑窗口显示/隐藏
                rules: {//校验规则
                    code: [{required: true, message: '编码为必填项', trigger: 'blur'}],
                    name: [{required: true, message: '名称为必填项', trigger: 'blur'}]
                }
            },
            created() {
                this.findPage();
            },
            methods: {
                //编辑的提交
                handleEdit() {
                    this.$refs["dataEditForm"].validate((valid) => {
                        //表单校验通过
                        if (valid) {
                            //打开全屏遮罩
                            var fullscreenLoading = this.$loading();

                            //提交
                            var url = "../setMeal/update?checkGroupIds=" + this.checkgroupIds;
                            axios.post(url, this.formData)
                                .then(res => {
                                    if (res.data.flag) {
                                        this.$message.success(res.data.msg);
                                        //关闭编辑窗口
                                        this.dialogFormVisible4Edit = false;
                                        //刷新列表
                                        this.findPage();
                                    } else {
                                        this.$message.warning(res.data.msg);
                                    }

                                    //关闭全屏遮罩
                                    fullscreenLoading.close();
                                })
                                .catch(error => {
                                    this.$message.error("请求出错：" + error);
                                    //关闭全屏遮罩
                                    fullscreenLoading.close();
                                });
                        }
                    });
                },
                //文件上传成功后的钩子
                handleAvatarSuccess(res) {
                    if (res.flag) {
                        // 回显图片
                        this.imageUrl = res.data.domain + res.data.imgName;
                        // 补充formData中的img值，
                        this.formData.img = res.data.imgName;
                    } else {
                        this.$message.error(res.data.msg);
                    }
                    //关闭遮罩
                    this.loading = false;
                },
                //上传图片之前执行
                beforeAvatarUpload(file) {
                    //打开遮罩
                    this.loading = true;

                    var isJPG = file.type === 'image/jpeg';
                    var isLt2M = file.size / 1024 / 1024 < 2;
                    if (!isJPG) {
                        this.$message.error('上传套餐图片只能是 JPG 格式!');
                    }
                    if (!isLt2M) {
                        this.$message.error('上传套餐图片大小不能超过 2MB!');
                    }
                    return isJPG && isLt2M;
                },
                //添加
                handleAdd() {
                    this.$refs["dataAddForm"].validate(valid => {
                        //表单校验通过
                        if (valid) {
                            //打开全屏遮罩
                            var fullscreenLoading = this.$loading();

                            //提交
                            var url = "../setMeal/add?checkGroupIds=" + this.checkgroupIds;
                            axios.post(url, this.formData)
                                .then(res => {
                                    if (res.data.flag) {
                                        this.$message.success(res.data.msg);
                                        //关闭新增窗口
                                        this.dialogFormVisible = false;
                                        //刷新列表
                                        this.findPage();
                                    } else {
                                        this.$message.warning(res.data.msg);
                                    }

                                    //关闭全屏遮罩
                                    fullscreenLoading.close();
                                })
                                .catch(error => {
                                    this.$message.error("请求出错：" + error);
                                    //关闭全屏遮罩
                                    fullscreenLoading.close();
                                });
                        }
                    });
                },
                //分页查询
                findPage() {
                    //打开全屏遮罩
                    var fullscreenLoading = this.$loading();

                    var url = "../setMeal/findPage/" + this.pageNow + "/" + this.pageSize
                        + "?query=" + this.queryString;
                    axios.get(url)
                        .then(res => {
                            if (res.data.flag) {
                                // 分页的结果
                                this.dataList = res.data.data.rows;
                                // 总记录
                                this.total = res.data.data.total;
                            } else {
                                this.$message.warning(res.data.msg);
                            }

                            //关闭全屏遮罩
                            fullscreenLoading.close();
                        })
                        .catch(error => {
                            this.$message.error("请求出错：" + error);
                            //关闭全屏遮罩
                            fullscreenLoading.close();
                        });
                },
                //加载检查组
                handleCheckGroup(flag, id) {
                    //打开全屏遮罩
                    var fullscreenLoading = this.$loading();

                    var url = "../checkGroup/findByQuery?query=" + this.queryCheckGroup;
                    axios.get(url)
                        .then(res => {
                            if (res.data.flag) {
                                this.tableData = res.data.data;

                                if (flag === 0) {
                                    //通过新增按钮打开面板时，清空选中状态
                                    this.$refs["add-multipleTable"].clearSelection();
                                } else if (flag === 1) {
                                    //通过编辑按钮打开面板时，清空选中状态，并加载套餐关联的检查组ID
                                    this.$refs["edit-multipleTable"].clearSelection();
                                    this.handleCheckGroupIds(id);
                                }
                            } else {
                                this.$message.warning(res.data.msg);
                            }

                            //关闭全屏遮罩
                            fullscreenLoading.close();
                        })
                        .catch(error => {
                            this.$message.error("请求出错：" + error);
                            //关闭全屏遮罩
                            fullscreenLoading.close();
                        });
                },
                //加载套餐关联的检查组
                handleCheckGroupIds(id) {
                    //打开全屏遮罩
                    var fullscreenLoading = this.$loading();
                    //通过id查询检查组
                    axios.get('../setMeal/findCheckGroupIds/' + id)
                        .then(res => {
                            if (res.data.flag) {
                                this.checkgroupIds = res.data.data;

                                //回显套餐关联的检查组
                                this.checkgroupIds.forEach(checkgroupId => {
                                    for (var i = 0; i < this.tableData.length; i++) {
                                        var checkGroup = this.tableData[i];
                                        if (checkgroupId == checkGroup.id) {
                                            this.$refs["edit-multipleTable"].toggleRowSelection(this.tableData[i], true);
                                        }
                                    }
                                });
                            } else {
                                this.$message.warning(res.data.msg);
                            }

                            //关闭全屏遮罩
                            fullscreenLoading.close();
                        })
                        .catch(error => {
                            this.$message.error("请求出错：" + error);
                            //关闭全屏遮罩
                            fullscreenLoading.close();
                        });
                },
                //复选框变化
                handleSelectionChange(arr) {
                    //获取id
                    this.checkgroupIds = [];
                    arr.forEach(el => {
                        this.checkgroupIds.push(el.id);
                    });
                },
                // 重置表单
                resetForm() {
                    //清空表单
                    this.formData = {};
                    //清空选中检查组
                    this.checkgroupIds = [];
                    //清空检查组查询条件
                    this.queryCheckGroup = "";
                    //默认选中套餐信息标签页
                    this.activeName = "first";
                    //清空图片地址
                    this.imageUrl = '';
                },
                // 弹出添加窗口
                handleCreate() {
                    // 重置表单
                    this.resetForm();
                    // 弹出添加窗口
                    this.dialogFormVisible = true;
                    //加载检查组列表
                    this.handleCheckGroup(0);
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    this.pageNow = currentPage;
                    this.findPage();
                },
                // 弹出编辑窗口
                handleUpdate(id) {
                    //重置表单，弹出编辑窗口
                    this.resetForm();
                    this.dialogFormVisible4Edit = true;
                    //加载被编辑的套餐
                    this.handleSetMeal(id);
                    //加载检查组列表
                    this.handleCheckGroup(1, id);
                },
                //加载被编辑的套餐
                handleSetMeal(id) {
                    //打开全屏遮罩
                    var fullscreenLoading = this.$loading();
                    //通过id查询检查组
                    axios.get('../setMeal/findById/' + id)
                        .then(res => {
                            if (res.data.flag) {
                                //套餐
                                this.formData = res.data.data.setmeal;
                                //图片
                                if (this.formData.img != null && this.formData.img != "") {
                                    this.imageUrl = res.data.data.domain + this.formData.img;
                                }
                            } else {
                                this.$message.warning(res.data.msg);
                            }

                            //关闭全屏遮罩
                            fullscreenLoading.close();
                        })
                        .catch(error => {
                            this.$message.error("请求出错：" + error);
                            //关闭全屏遮罩
                            fullscreenLoading.close();
                        });
                },
                // 删除
                handleDelete(id) {
                    this.$confirm('此操作将【永久删除】该套餐, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        //打开全屏遮罩
                        var fullscreenLoading = this.$loading();

                        // 提交删除
                        axios.delete("../setMeal/deleteById/" + id)
                            .then(res => {
                                if (res.data.flag) {
                                    this.$message.success(res.data.msg);
                                    //刷新列表
                                    this.findPage();
                                } else {
                                    this.$message.warning(res.data.msg);
                                }

                                //关闭全屏遮罩
                                fullscreenLoading.close();
                            })
                            .catch(error => {
                                this.$message.error("请求出错：" + error);
                                //关闭全屏遮罩
                                fullscreenLoading.close();
                            });
                    }).catch(() => {
                    });
                }
            }
        })
    </script>
</html>
